//
//  RegisterViewController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "RegisterViewController.h"
#import "SVProgressHUD.h"

@interface RegisterViewController ()

@property (nonatomic, strong) UITextField * phoneNumberTF; //手机号
@property (nonatomic, strong) UITextField * verfiTF;       //验证码
@property (nonatomic, strong) UIButton    * timerButton;   //倒计时
@property (nonatomic, strong) UIButton    * boxButton;     //点击同意box
@property (nonatomic, strong) UILabel     * agressLabel;   //同意协议字符串
@property (nonatomic, strong) UIButton    * loginButton;   //登录下一步

@property (nonatomic, strong) NSTimer *timer;//计时器

@property (nonatomic, assign) NSInteger secondes;//倒计时时间

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    self.title = @"注册";
    //添加控件
    [self.view addSubview:self.phoneNumberTF];
    [self.view addSubview:self.verfiTF];
    [self.view addSubview:self.timerButton];
    [self.view addSubview:self.boxButton];
    [self.view addSubview:self.agressLabel];
    [self.view addSubview:self.loginButton];
    //约束控制
    [self.phoneNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64+40);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.timerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.phoneNumberTF.mas_bottom).offset(30);
        make.right.mas_equalTo(self.phoneNumberTF.mas_right);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(100);
        
    }];
    
    [self.verfiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.timerButton.mas_top);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.timerButton.mas_left).offset(-20);
    }];
    
    //同意协议内容
    [self.boxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.verfiTF.mas_bottom).offset(45);
        make.left.mas_equalTo(self.verfiTF.mas_left).offset(40);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        
    }];
    
    [self.agressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.boxButton);
        make.left.mas_equalTo(self.boxButton.mas_right).offset(10);
        make.height.equalTo(self.boxButton);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.boxButton.mas_bottom).offset(20);
        make.height.mas_equalTo(45);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
        
    }];

}



#pragma mark  - Lazy
//电话号码 文本框
- (UITextField *)phoneNumberTF{
    
    if (_phoneNumberTF == nil) {
        _phoneNumberTF = [[UITextField alloc] init];
        _phoneNumberTF.placeholder = @"请输入您的手机号码";
        _phoneNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
        _phoneNumberTF.keyboardType = UIKeyboardTypePhonePad;//设置键盘的样式
        [_phoneNumberTF becomeFirstResponder ];
        
    }
    return _phoneNumberTF;
    
}
//验证码文本框
- (UITextField *)verfiTF{
    if (_verfiTF == nil) {
        _verfiTF = [[UITextField alloc] init];
        _verfiTF.placeholder = @"请输入验证码";
        _verfiTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _verfiTF.borderStyle = UITextBorderStyleRoundedRect;
        _verfiTF.keyboardType = UIKeyboardTypePhonePad;
        [_verfiTF becomeFirstResponder];
    }
    return _verfiTF;
}

//单选按钮
- (UIButton *)boxButton{
    
    if (_boxButton == nil) {
        
        _boxButton = [[UIButton alloc] init];
        [_boxButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
        [_boxButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
        [_boxButton addTarget:self action:@selector(clickBoxButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _boxButton;
    
}

//本人同意并阅读 用户协议
-(UILabel *)agressLabel{
    
    if (_agressLabel == nil) {
        _agressLabel = [[UILabel alloc] init];
        
        _agressLabel.font = FONT(13);
        
        NSString *agressText = @"本人同意并阅读 用户协议";
        NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithString:agressText];
        
        NSRange range = [agressText rangeOfString:@"用户协议"];
        [attM addAttributes:@{
                              NSFontAttributeName:FONT(14),
                              NSForegroundColorAttributeName:BLUE_COLOR
                              } range:range];
        
        _agressLabel.attributedText = attM;
        
    }
    return _agressLabel;
}


//获取验证码

-(UIButton *)timerButton{
    
    if (_timerButton == nil) {
        
        _timerButton = [[UIButton alloc] init];
        [_timerButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _timerButton.backgroundColor = [UIColor lightGrayColor];
        _timerButton.layer.cornerRadius = 3;
        _timerButton.layer.masksToBounds = YES;
        _timerButton.titleLabel.font = FONT(16);
        [_timerButton addTarget:self action:@selector(clickTimerButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timerButton;
}

// 注册按钮
-(UIButton *)loginButton{
    
    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc] init];
        [_loginButton setTitle:@"注册" forState:UIControlStateNormal];
        _loginButton.backgroundColor = kThemeColor;
        _loginButton.titleLabel.font = FONT(20);
        [_loginButton addTarget:self action:@selector(clickRegisterButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

#pragma mark -
#pragma mark -  点击单选按钮
- (void)clickBoxButton:(UIButton *)button{
    button.selected = !button.isSelected;

}
#pragma mark -
#pragma mark - 点击获取验证码

- (void)clickTimerButton:(UIButton *)button{
    
    //在电话号码为空的情况下,button 点击失效
    if (_phoneNumberTF.text.length != 0) {
        self.secondes = 1;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerCut) userInfo:nil repeats:YES];
        [self.timer fire];
    }else{
        button.selected = NO;
    }
    
    
}

/** 倒计时开始 */
- (void)timerCut{

    NSString *stateString = nil;

    if (self.secondes<=0) {
        
        self.timerButton.userInteractionEnabled = YES;
        
        stateString = @"获取验证码";
        
        [self.timer invalidate];
        self.timer = nil;
        
    }else{
        
        self.timerButton.userInteractionEnabled = NO;
        
        stateString = [NSString stringWithFormat:@"%lds",self.secondes];
    }
    
    [self.timerButton setTitle:stateString forState:UIControlStateNormal];
    
    self.secondes -= 1;
}

//计时器失效
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [self.timer invalidate];
    self.timer = nil;
    
}


//开始注册
- (void)clickRegisterButton:(UIButton *)button{
    
    NSLog(@"%d",_boxButton.isSelected);
    
    if ((_phoneNumberTF.text.length != 0)&&(_verfiTF.text.length != 0)&&(_boxButton.isSelected)) {
        
        //注册成功
        [SVProgressHUD showSuccessWithStatus:@"注册成功!"];
        [SVProgressHUD setFadeInAnimationDuration:0];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD dismissWithDelay:1];

    }else{
        if (_phoneNumberTF.text.length ==0) {
            [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
            [SVProgressHUD setFadeInAnimationDuration:0];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD dismissWithDelay:1];
            
        }else if(_phoneNumberTF.text.length ==0){
            [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
            [SVProgressHUD setFadeInAnimationDuration:0];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD dismissWithDelay:1];
        }else if(!_boxButton.isSelected){
            [SVProgressHUD showErrorWithStatus:@"请勾选是否允许用户协议"];
            [SVProgressHUD setFadeInAnimationDuration:0];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD dismissWithDelay:1];
        }
    }
    
}

@end
